package com.sojson.config.request;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import com.sojson.config.task.bean.TdHandler;

/**
 * 请求配置类
 *
 * @author liu
 * @date 2022-01-11
 */
@Configuration
public class RequestConfig {

    /**
     * 线程装饰器,类似切面
     *
     * @return
     */
    @Bean
    public TdHandler<RequestAttributes> requestTdHandler() {
        return new TdHandler<RequestAttributes>() {
            @Override
            public RequestAttributes prefix() {
                return RequestContextHolder.getRequestAttributes();
            }

            @Override
            public void runByThread(Object obj) {
                RequestContextHolder.setRequestAttributes((RequestAttributes)obj);
            }

            @Override
            public void finallys() {
                RequestContextHolder.resetRequestAttributes();
            }

            @Override
            public int getSort() {
                return 1;
            }
        };
    }

}